﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.SEDIC.EN;
using MJM.CBD.Conn;

namespace MJM.SEDIC.AD
{
    public static class SubMenuAD
    {
        public static int Insertar(SubMenuEN obj)
        {
            int codigo=1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_i_T_MAES_SUBMEN_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vMENU_P_inID", obj.Menu));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chNOMMEN", obj.Nombre));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chDESMEN", obj.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chNOMICO", obj.Icono));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_biICOMEN", obj.Archivo));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chURLMEN", obj.URL));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_inFLAPRI", obj.Prioritario));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_inORDEN", obj.Orden));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_inFLAELI", obj.Estado));
                SqlParameter prm = new SqlParameter();
                prm.Direction = ParameterDirection.ReturnValue;
                prm.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(prm);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                codigo = Convert.ToInt32(prm.Value);
                cmd.Connection.Close();
                conn.Dispose();
                cmd.Dispose();
            }
            catch(Exception ex)
            { 
                codigo=0;
                conn.Dispose();
                cmd.Dispose();
            }
            return codigo;
        }

        public static List<SubMenuEN> Listar(SubMenuEN obj)
        {
            List<SubMenuEN> lst = new List<SubMenuEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_s_T_MAES_SUBMEN_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vMENU_P_inID",obj.Menu));

                cmd.Connection.Open();
                var dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var item = new SubMenuEN();
                    item.Archivo = (byte[])dr[5];
                    item.Descripcion = dr.GetString(dr.GetOrdinal("SUBMEN_chDESMEN"));
                    item.Estado = dr.GetInt32(dr.GetOrdinal("SUBMEN_inFLAELI"));
                    item.Icono = dr.GetString(dr.GetOrdinal("SUBMEN_chNOMICO"));
                    item.Menu = dr.GetInt32(dr.GetOrdinal("MENU_P_inID"));
                    item.Nombre = dr.GetString(dr.GetOrdinal("SUBMEN_chNOMMEN"));
                    item.Orden = dr.GetInt32(dr.GetOrdinal("SUBMEN_inORDEN"));
                    item.Prioritario = dr.GetInt32(dr.GetOrdinal("SUBMEN_inFLAPRI"));
                    if (item.Prioritario > 0)
                        item.Tipo = "SI";
                    else
                        item.Tipo = "NO";
                    item.SubMenu = dr.GetInt32(dr.GetOrdinal("SUBMEN_P_inID"));
                    item.URL = dr.GetString(dr.GetOrdinal("SUBMEN_chURLMEN"));
                    item.SMenu = dr.GetString(dr.GetOrdinal("MENU_chNOMMEN"));
                    lst.Add(item);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }

        public static int Actualizar(SubMenuEN obj)
        {
            int codigo = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaVecinos());
            SqlCommand cmd = new SqlCommand("sp_u_T_MAES_SUBMEN_Actualizar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_P_inID", obj.SubMenu));
                cmd.Parameters.Add(new SqlParameter("@vMENU_P_inID", obj.Menu));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chNOMMEN", obj.Nombre));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chDESMEN", obj.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chNOMICO", obj.Icono));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_biICOMEN", obj.Archivo));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_chURLMEN", obj.URL));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_inFLAPRI", obj.Prioritario));
                cmd.Parameters.Add(new SqlParameter("@vSUBMEN_inORDEN", obj.Orden));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                codigo = 0;
                conn.Dispose();
                cmd.Dispose();
            }
            return codigo;
        }

    }
}
